package com.ef.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ELeadsDealType;
import com.ef.dao.LeadsDealHistoryDao;
import com.ef.domain.Leads;
import com.ef.domain.LeadsDealHistory;
import com.ef.service.LeadsDealHistoryService;

public class LeadsDealHistoryServiceImpl extends BaseServiceImpl implements
		LeadsDealHistoryService {

	private LeadsDealHistoryDao leadsDealHistoryDao;

	public List<LeadsDealHistory> findByLeads(Leads leads) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(LeadsDealHistory.class);
		detachedCriteria.add(Restrictions.eq("leads", leads));
		detachedCriteria.addOrder(Order.desc("dealTime"));
		return leadsDealHistoryDao.findAllByCriteria(detachedCriteria);
	}

	public List<LeadsDealHistory> findByLeads(Leads leads, ELeadsDealType type) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(LeadsDealHistory.class);
		// detachedCriteria.createCriteria("leads").add(Restrictions.eq("id",
		// leads.getId()));
		detachedCriteria.add(Restrictions.eq("leads", leads));
		detachedCriteria.add(Restrictions.eq("type", type));
		detachedCriteria.addOrder(Order.desc("dealTime"));
		return leadsDealHistoryDao.findAllByCriteria(detachedCriteria);
	}

	public List<LeadsDealHistory> findShowHistoryByLeads(Leads leads) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(LeadsDealHistory.class);
		// detachedCriteria.createCriteria("leads").add(Restrictions.eq("id",
		// leads.getId()));
		detachedCriteria.add(Restrictions.eq("leads", leads));
		detachedCriteria.add(Restrictions.or(Restrictions.eq("type", ELeadsDealType.Showup),
				Restrictions.eq("type", ELeadsDealType.UnShowup)));
		detachedCriteria.addOrder(Order.desc("dealTime"));
		return leadsDealHistoryDao.findAllByCriteria(detachedCriteria);
	}

	public void saveOrUpdate(LeadsDealHistory entity) {
		leadsDealHistoryDao.save(entity);
	}

	public LeadsDealHistoryDao getLeadsDealHistoryDao() {
		return leadsDealHistoryDao;
	}

	public void setLeadsDealHistoryDao(LeadsDealHistoryDao leadsDealHistoryDao) {
		this.leadsDealHistoryDao = leadsDealHistoryDao;
	}

}
